package JavaUtils.web.req.io;

import JavaUtils.CSUtils.Log.CLog;
import JavaUtils.web.IWebCallback;
import JavaUtils.web.ReqInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Req_fileSave {
    static final String tag = "Req_fileSave";

    public static IWebCallback WriteAllBytes() {
        IWebCallback fileUploadEvent = new IWebCallback() {
            @Override
            public void MsgHandle(ReqInfo reqInfo) throws IOException {
                String filePath = reqInfo.dataMap.get("filePath");
                reqInfo.resp_txt = "数据保存完成了";
                File file = new File(filePath);
                File dir = new File(file.getParent());
                if (!dir.exists()) {
                    dir.mkdirs(); //创建目录
                }
                if (file.exists()) { // 存在
                    file.delete();

                }
                if (file.createNewFile()) {
                    CLog.f(tag, "创建文件成功！.....%s", filePath);
                } else {
                    CLog.exf(tag, "文件创建失败.....%s", filePath);
                }
                FileOutputStream fos = new FileOutputStream(file);
                for (byte[] bytes : reqInfo.dataBytesList) {
                    fos.write(bytes);
                }
                fos.flush();
                CLog.f(tag, "数据..写入完成.......size=%s....fileLen=%s", reqInfo.dataBytesList.size(), file.length());
            }
        };
        return fileUploadEvent;
    }

}
